//Generating Age_group//
use "C:\Users\user\Desktop\stat session2020\rawDataLogan.dta"
gen Age_groupMat=1 if  maternalage<20 &  maternalage!=.
replace Age_groupMat=2 if  maternalage>=20 &  maternalage<=34
replace Age_groupMat=3 if  maternalage>=35 &  maternalage<=49
tab Age_groupMat
drop Age_groupMater
save,replace
//Age-at First Pregnancy//
gen Age_FirstPregnancy1=1 if ageatfirstpregnancy<20 & ageatfirstpregnancy!=.
replace  Age_FirstPregnancy1=2 ageatfirstpregnancy>=20 & ageatfirstpregnancy<=34
replace Age_FirstPregnancy1=2 ageatfirstpregnancy>=20 & ageatfirstpregnancy<=34
tab ageatfirstpregnancy
codebook ageatfirstpregnancy
replace Age_FirstPregnancy1=2 if ageatfirstpregnancy>=20 & ageatfirstpregnancy!=.
tab Age_FirstPregnancy1
label define Age_FirstPregnancy1 1 "<20" 2 ">20"
label values Age_FirstPregnancy1 Age_FirstPregnancy1
save,replace
codebook Age_FirstPregnancy1

//Maternal Education//
encode  maternaleducation, gen (Maternaleduc1)
tab Maternaleduc1
codebook Maternaleduc1
save,replace
//Occupation level//
gen occupation1=1 if occupation=="Government/private employee"
tab occupation1
replace occupation1=2 if  occupation=="House wife"
replace occupation1=3 if  occupation=="Merchant/business"
replace occupation1=4 if  occupation!="Merchant/business" & occupation!="Government/private employee" & occupation!="House wife" & occupation!=""
tab occupation1
label define gabriel 1 "Goverment/private" 2 "House wife" 3 "Merchant/business" 4 "Others"
label values occupation1 gabriel
tab occupation1
//County of Residence//
gen Countyresidence1=1 if  countyofresidence=="Nairobi"
replace Countyresidence1=2 if  countyofresidence=="Kiambu"
replace Countyresidence1=3 if  countyofresidence=="Machakos"
.replace Countyresidence1=4 if  countyofresidence=="Kajiado"
replace Countyresidence1=4 if  countyofresidence=="Kajiado"
replace Countyresidence1=5 if  countyofresidence=="Muranga"
replace Countyresidence1=6 if  countyofresidence!="Kajiado" & countyofresidence!="Muranga" & countyofresidence!="Machakos" & countyofresidence!="Kiambu" & countyofresidence!="Nairobi" & countyofresidence!=""
save,replace
tab Countyresidence1
label define Countyresidence1 1 "Nairobi" 2 "Kiambu" 3 "Machakos" 4 "Kajiado" 5 "Muranga" 6 "Others"
label values Countyresidence1 Countyresidence1
save,replace
codebook Countyresidence1
//Specific area of residence//
encode  specificplacearealivedincountyof, gen(Specific_AreaResid)
codebook Specific_AreaResid
save,replace
label variable Specific_AreaResid "Specific area of residence"
save,replace
//Number of ANC_vISITS//
gen Number_AncVisit=1 if  numberofancvisit==0
replace Number_AncVisit=2 if  numberofancvisit==1
replace Number_AncVisit=3 if  numberofancvisit>1 & numberofancvisit<=3
replace Number_AncVisit=4 if  numberofancvisit>=4 & numberofancvisit!=.
save,replace
tab Number_AncVisit
label define Number_AncVisit 1 "No AncVisit" 2 "1 AncVisit" 3 "2-3 AncVisits" 4 "4+ AncVisits"
label values Number_AncVisit Number_AncVisit
save,replace
codebook Number_AncVisit
// Gravidity//
gen Gravidity1=1 if  gravidity==1
replace Gravidity1=2 if gravidity>=2 & gravidity!=.
tab Gravidity1
label define Gravidity1 1 "Primigravida" 2 "Multigravida"
label values Gravidity1 Gravidity1
codebook Gravidity1
save, replace
//Parity//
gen Parity1=1 if  parity==0
replace Parity1=2 if  parity==1
replace Parity1=3 if  parity>=2 & parity !=.
tab Parity1
label define Parity1 1 "Nulliparous" 2 "Primiparous" 3 "Multiparous"
label values Parity1 Parity1
codebook Parity1
save,replace
//Time/Trimester of First ANC Visit//
gen trimester_FirstAncVisit=1 if Tri==1 | Tri==2
replace trimester_FirstAncVisit=2 if Tri==3
replace trimester_FirstAncVisit=3 if Tri==4
label define fred 1 "First trimester and none" 2 "Second Trimester" 3 "Third Trimester"
label values trimester_FirstAncVisit fred
codebook trimester_FirstAncVisit
save,replace
//Mode of Delivery//
encode  modeofdelivery, gen( modeofdelivery1)
codebook  modeofdelivery1
save,replace
//Child Sex//
gen Child_Sex=1 if  childsex==" Male "
gen Child_Sex1=1 if  childsex=="Male"
drop Child_Sex
replace  Child_Sex1=2 if  childsex=="Female"
replace  Child_Sex1=3 if  childsex=="(Male & Female/ Male & Male/ Female & Female"
replace  Child_Sex1=3 if  childsex=="Female, female"|childsex=="Male, female"|childsex=="Male, male"
tab Child_Sex1
label define ch 1 "Male" 2 "Female" 3 "(Male & Male/Female & Female/ Male & Female"
label values Child_Sex1 ch
codebook Child_Sex1
save, replace
//Multiplicity of Pregnancy//
encode  multiplicityofpregnancy,gen( multiplicityofpregnancy1)
codebook  multiplicityofpregnancy1
save,replace
//Featus Status//
encode fetalstatus, gen( fetalstatus1)
codebook fetalstatus1
save,replace
//BirthWeight//
gen Birthweight1=1 if  birthweight<2500 &  birthweight!=.
replace Birthweight1=2 if  birthweight>=2500 &  birthweight<=4200
replace Birthweight1=3 if  birthweight>4200 &  birthweight!=.
tab Birthweight1
label define b 1 "Low birthweight" 2 "Normal birth weight" 3 "High birthweight"
label values Birthweight1 b
save, replace
codebook  Birthweight1
tab  birthweight
tab birthweight, m
//Apgarscore at fiveminutes//
gen apgarscore1=1 if  apgarscoreatfiveminutes>=7 & apgarscoreatfiveminutes<=10
replace apgarscore1=2 if  apgarscoreatfiveminutes>=4 & apgarscoreatfiveminutes<=6
replace apgarscore1=3 if  apgarscoreatfiveminutes<4 &  apgarscoreatfiveminutes!=.
tab apgarscore1
tab apgarscore1,m
label define apa 1 "Normal" 2 "Moderately depressed" 3 "Severely depressed"
label values apgarscore1 apa
save,replace
codebook apgarscore1
//Haemoglobin level on Admission for delivery//
gen haemoglobin1=1 if  hemoglobinlevelonadmissionfordel>=11 & hemoglobinlevelonadmissionfordel!=.
replace haemoglobin1=2 if  hemoglobinlevelonadmissionfordel>=7 & hemoglobinlevelonadmissionfordel<=10.9
replace haemoglobin1=3 if  hemoglobinlevelonadmissionfordel<7 & hemoglobinlevelonadmissionfordel!=.
tab haemoglobin1
label define h 1 "Normal" 2 "Mild-Moderate Level anaemia" 3 "Severe anamea"
label values haemoglobin1 h
tab haemoglobin1,m
save,replace
//Number of PUS CELLS ON ADMISSION FOR DELIVERY//
gen  nupucells1=1 if  numberofpuscellsinurineonadmissi >0 & numberofpuscellsinurineonadmissi<=5
replace  nupucells1=2 if  numberofpuscellsinurineonadmissi >=6 & numberofpuscellsinurineonadmissi<=10
replace  nupucells1=3 if  numberofpuscellsinurineonadmissi >10 & numberofpuscellsinurineonadmissi!=.
tab nupucells1
label define nu 1 "Normal" 2 "Mild-Moderate UTI in pregnancy" 3 "Severe UTI in pregnancy"
label values nupucells1 nu
tab nupucells1
save, replace
//Tobacco use//
use "C:\Users\user\Desktop\stat session2020\rawDataLogan.dta"
br
encode tobaccouse, gen( tobaccouse1)
codebook tobaccouse1
save,replace
//Tobacco use//
encode timeoftobaccouse, gen( timeoftobaccouse1)
codebook timeoftobaccouse1
//alcohol use//
codebook timeoftobaccouse1
encode  alcoholuse,gen( alcoholuse1)
save,replace
encode  timeofalcoholuse,gen( timeofalcoholuse1)
save,replace
codebook alcoholuse1
codebook timeofalcoholuse1
//Diabetis personal history//
encode  diabetespersonalhistory, gen( diabetespersonalhistory1)
codebook  diabetespersonalhistory1
save,replace
//diabetis familly history//
encode  diabetesfamilyhistory,gen( diabetesfamilyhistory1)
codebook diabetesfamilyhistory1
save,replace
//Hypertension personal history//
encode hypertensionpersonalhistory,gen( hypertensionpersonalhistory1)
codebook hypertensionpersonalhistory1
save, replace
//Hypertension Family history//
save, replace
encode  hypertensionfamilyhistory,gen( hypertensionfamilyhistory1)
codebook hypertensionfamilyhistory1
save,replace
//traditional treatment use//
tab  traditionaltreatmentuse
encode  traditionaltreatmentuse,gen( traditionaltreatmentuse1)
codebook traditionaltreatmentuse1
//SpecifiTraditional treatment//
gen  specificTreatment=1 if  specifictreatmentifusetraditiona=="Traditional/local herbs"
replace specificTreatment=2 if  specifictreatmentifusetraditiona=="Ayurvedic/chinese medicine"
replace specificTreatment=3 if  specifictreatmentifusetraditiona=="Traditional/local chalk"
replace specificTreatment=4 if  specifictreatmentifusetraditiona== "Traditional/local ointment"
codebook specificTreatment
label define treat 1 "Traditional/local herbs" 2 "Ayurvedic/chinese medicine" 3 "Traditional/local chalk" 4 "Traditional/local ointment"
label values specificTreatment treat
codebook specificTreatment
save,replace
//Purpose Treatment//
encode purposeifusetraditionaltreatment, gen(Purposetreatment)
codebook Purposetreatment
save,replace
//Ethnicity//
tab ethnicity
replace ethnicity=trim(ethnicity)
gen EthnKenya=1 if inlist( ethnicity,"Burundian","Somali","Tanzanian","Ugandan")
replace EthnKenya=2 if ethnicity!="" & EthnKenya!=1
br EthnKenya ethnicity if EthnKenya==1
label define eth 1 "Non-Kenyan" 2 "Kenyan"
label values EthnKenya eth
//Religion//
encode  religion, gen( religion1)
tab religion
codebook religion1
//Disease/Outcome variable////Hospital Type//
gen Disease1=1 if  Disease==1
replace Disease1=0 if  Disease==2
label define Disease1 1 "Case" 0 "Control"
label values Disease1 Disease1
codebook Disease1
save,replace
//Study Hospital//
tab studyhospital
encode  studyhospital, gen(studyhospital1)
codebook studyhospital1
save,replace
//LOGISTIC REGRESSION ANALYSIS//
//logistic regression of Demographic factors//
xtmelogit  Disease1 i. Age_groupMat i.Education_Leve ib2.Marital1 i. occupation1 ib2.Residence_county1 ib2.Specifiarea_Residence1
ib6. Ethnicity1 i. religion1||  studyhospital1:
xtmelogit, or
//Logistic regression for Reproductive and obstetric//
xtmelogit  Disease1 ib2.Age_FirstMarriage ib2.Age_FirstPregnancy1 ib2.Number_ANCvisits i.trimester_FirstAncVisit i.Gravidity1
ib3.Parity1 ib3.Child_Sex1||studyhospital1:
xtmelogit, or
//Logistic Regression for Clinical factors//
 xtmelogit  Disease1 i.nupucells1 i.haemoglobin12|| studyhospital1:
xtmelogit, or
//Logistic Regression for Behavioral and Family History-Related Factors//
xtmelogit  Disease1 i.alcoholuse1 i.tobaccouse1 i.hypertensionpersonalhistory1 i.hypertensionfamilyhistory1 i.diabetesfamilyh
istory1 i.traditionaltreatmentuse1||studyhospital1:
xtmelogit, or
//Multivariate Mixed Effect Logistic regression Modell with interaction//
//With ANC and Haemoglobin//
xtmelogit  Disease1 i.Age_groupMat i.occupation1 ib2.Residence_county1 ib6. Ethnicity1 ib2.Age_FirstPregnancy1 ib2.Number_ANCvisits i.trimester_FirstAncVisit ib3.Parity1 i.haemoglobin12 i.tobaccouse1 i.hypertensionpersonalhistory1 i.hypertensionfamilyhistory1 i.Ethnicity1#i.hypertensionfamilyhistory1 ||studyhospital1:
xtmelogit,or

//Multivariate Mixed Effect Logistic regression Modell withithout interaction//
xtmelogit  Disease1 i.Age_groupMat ib4.occupation1 ib2.Residence_county1 ib5.Ethnicity12 ib2.Age_FirstPregnancy1 ib2.Number_ANCvisits i.trimester_FirstAncVisit ib3.Parity1 i.haemoglobin12 i.tobaccouse1 i.hypertensionpersonalhistory1 i.hypertensionfamilyhistory1 ||studyhospital1:
xtmelogit,or
//Goodness of fi test (3)//
predict yhat, mu
local ngroups 3
xtile quantil=yhat, nq(3)
collapse (sum) Disease1 yhat, by(quantil)
list quantil Disease1 yhat, noobs clean
graph twoway connect Disease1 yhat|| line yhat yhat, sort
//Goodness of fi test (10)//
predict yh, mu
local ngro 10
xtile quan=yh, nq(10)
collapse (sum) Disease1 yh, by(quan)
list quan Disease1 yh, noobs clean
graph twoway connect Disease1 yh|| line yh yh, sort
//Manuscript model fit//
predict y, mu
local ng 10
xtile qu=y, nq(10)
collapse (sum) Disease1 y, by(qu)
list qu Disease1 y, noobs clean
//REVIEW//


